package com.pccw.portlet.util.excel.example;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.pccw.portlet.util.excel.importutil.ExcelImporter;
import com.pccw.portlet.util.excel.importutil.ImportedExcelData;

public class ExcelImportExample {
/*	public static void main(String[] args){
		testExcelImporter();
		
		System.out.println("All Test Finished!");
	}*/
	
	public static Map testExcelImporter(){
		Map lineData = null;
		//模板文件
		String templateFilePath = "e:/import_validate_box.xls"; 
		//上传的文件
		String importFilePath = "e:/import_data_box.xls";
		
		ExcelImporter importer = new ExcelImporter(templateFilePath, importFilePath);
		
		boolean result = importer.importExcel();
		if(result == true){
			System.out.println("import successful!");
			
			ImportedExcelData importedData = importer.getImportedExcelData();
			//工作表
			int sheetCount = importedData.getImportedSheetCount(); 
			System.out.println(sheetCount + " sheets imported!");
			
			//循环工作表
			for(int i = 0; i < sheetCount; i++){
				//工作表名
//				String sheetName = importedData.getNameOfSheet(i); 
				//工作表表头数据
				Map sheetHeadData = importedData.getHeadDataOfSheet(i);
				//获取表头数据，存入Map
				if(sheetHeadData != null){
					Set entrySet1 = sheetHeadData.entrySet();
					Iterator it1 = entrySet1.iterator();
					while(it1.hasNext()){
						Map.Entry entry = (Map.Entry)it1.next();
						System.out.println(entry.getKey() + " = " + entry.getValue());
					}
				}
				
				// 获取行数据
				List sheetDetailLines = importedData.getDetailDataOfSheet(i);
				if(sheetDetailLines == null || sheetDetailLines.size() == 0){
					System.out.println("当前工作表没数据");
				}
				
				// 循环行数据
				for(int j = 0; sheetDetailLines != null && j < sheetDetailLines.size(); j++){
					lineData = (Map)sheetDetailLines.get(j); 
//					System.out.println("imported " + lineData.size() + " cells from line: " + j);
//					Set entrySet2 = lineData.entrySet();
//					Iterator it2 = entrySet2.iterator();
//					while(it2.hasNext()){
//						Map.Entry entry = (Map.Entry)it2.next();
//						System.out.println(entry.getKey() + " = " + entry.getValue());
//					}
					
//					Double distributionAmount = (Double)lineData.get("distributionAmount");

				}
				lineData.put("lineSize", lineData.size());
			}
			
			/*//备份
			String backupPath = "e:/backupExcel.xls"; 
			File backupFile = importer.backupExcel(backupPath);
			if(backupFile == null){
				System.out.println("backup original excel failed!");
			}
			else{
				System.out.println("original excel backuped to " + backupFile.getAbsolutePath());
			}
			//备份
			String backupWorkbookPath = "e:/backupWorkbook.xls"; 
			backupFile = importer.backupWorkbookToExcel(backupWorkbookPath);
			if(backupFile == null){
				System.out.println("backup workbook failed!");
			}
			else{
				System.out.println("workbook backuped to " + backupFile.getAbsolutePath());
			}*/
		}
		else{
			//导入失败
			System.out.println("import failed!");
			List errorList = importer.getErrorList();
			for(int i = 0; errorList != null && i < errorList.size(); i++){
				System.out.println((String)errorList.get(i));
			}
			
			File errorFile = importer.getErrorFile();
			if(errorFile != null){
				String errorFilePath = errorFile.getAbsolutePath();
				System.out.println("Error File is: " + errorFilePath);
			}
		}
		return lineData;
	}
}
